package com.bjsxt.collection;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class HashSetTest {
    public static void main(String[] args) {
        Set<String> set =new HashSet<>();
        set.add("u");
        set.add("t");
        set.add("u");
        set.add("s");
        System.out.println(set);

        Set<Users> set1 =new HashSet<>();
        Users u = new Users("sa",19);
        Users u1 = new Users("sa2",18);
        Users u2 = new Users("sa2",18);

        set1.add(u);
        set1.add(u1);
        set1.add(u2);
        System.out.println(set1);
    }
}
class Users implements Comparable<Users>{
    private String username;
    private int userage;

    public String getUsername() {
        return username;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Users users = (Users) o;
        return userage == users.userage && Objects.equals(username, users.username);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, userage);
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getUserage() {
        return userage;
    }

    public void setUserage(int userage) {
        this.userage = userage;
    }

    public Users(String username, int userage) {
        this.username = username;
        this.userage = userage;
    }

    @Override
    public String toString() {
        return "Users{" +
                "username='" + username + '\'' +
                ", userage=" + userage +
                '}';
    }

    @Override
    public int compareTo(Users o) {
        if (this.userage>o.getUserage()){
            return -1;
        }
        return 1;
    }
}
